﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TeamWorkNet.Base;
using TeamWorkNet.Handler;
using TeamWorkNet.Model;

namespace TeamWorkNetTestUtility
{
  public partial class Form1 : Form
  {
    private TeamWorkClient baseclient;
    private List<Project> Projects; 
    public Form1()
    {
      InitializeComponent();
      baseclient = new TeamWorkClient();
      baseclient.Init(textBox1.Text, textBox2.Text);
    }

    public void InitBaseClient()
    {
      baseclient = new TeamWorkClient();
      baseclient.Init(textBox1.Text, textBox2.Text);
    }

    private async void button1_Click(object sender, EventArgs e)
    {
      if (baseclient == null) InitBaseClient();
      var data = await new ProjectHandler(baseclient).GetAllProjectsAsync();
      Projects = data.projects;
      foreach (var item in data.projects)
      {
        treeView1.Nodes.Add(item.id,item.name);
      }
      
    }

    private async void button2_Click(object sender, EventArgs e)
    {
      if (baseclient == null) InitBaseClient();

      var handler = new AccountHandler(baseclient);
      var data2 = await handler.GetAccountDetails();
      var data = await handler.GetAuthenticationtDetails();
    }

    private void toolStripMenuItem1_Click(object sender, EventArgs e)
    {

    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Application.Exit();
    }

    private Project project;
    private async void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
      //if (baseclient == null) InitBaseClient();
      //project = Projects.Find(p => p.id == e.Node.Name);

      //var details = await new ProjectHandler(baseclient).GetProject(int.Parse(e.Node.Name), true,true);

      //treeView2.Nodes.Clear();
      //if (details.project.Tasklists == null)
      //{
      //  treeView2.Nodes.Add("No Tasklists found or tasklists have not been loaded");
      //}
      //else { 
      //foreach (var prj in details.project.Tasklists)
      //{

      //  var node = treeView2.Nodes.Add(prj.Id, prj.Name);
      //  foreach (var task in prj.TodoItems)
      //  {
      //    node.Nodes.Add(task.Id, task.Content);
      //  }

      //}
      //}
    }

    private async void button3_Click(object sender, EventArgs e)
    {
      FileDialog fd = new OpenFileDialog();
      fd.ShowDialog();

      var handler = new FileHandler(baseclient);
         baseclient.Init(textBox1.Text,textBox2.Text);

        var data = await handler.UploadFileToProject(102887,"No Description Just a test", fd.FileName, System.IO.Path.GetFileName(fd.FileName));

    }
  }
}
